package programm;

import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

/**
 * Makes a FileChooser.
 * @author jm
 *
 */
public class OwnFileChooser
{

	private String sPath;

	/**
	 * Constructor
	 * @param einschraenkung You only want to search for .dll, then use ".dll"
	 * @param title The title the FileChooser bar should have.
	 */
	public OwnFileChooser(final String einschraenkung, String title)
	{
		// Create a file chooser
		final JFileChooser fc = new JFileChooser();
		fc.setFileFilter(new FileFilter()
		{
			@Override
			public boolean accept(File f)
			{
				return f.isDirectory()
						|| f.getName().toLowerCase().endsWith(einschraenkung);
			}

			@Override
			public String getDescription()
			{
				return "(*" + einschraenkung + ")";
			}
		});
		int state = 0;
		fc.setDialogTitle(title);
		do
		{
			System.out.println("filechooser in do while schleife: ObjektHash: " + this.toString());
			state = fc.showOpenDialog(null);
			if (state == JFileChooser.CANCEL_OPTION)
				break;
			File file = fc.getSelectedFile();
			sPath = file.getAbsolutePath().replace('\\', '/');

			state = 0;
			
			System.out.println("gewähltes file: " + fc.getSelectedFile().getName());
		} while (fc.getSelectedFile().getName()
				.indexOf(einschraenkung) == -1);
	}

	/**
	 * Returns the Path to the selected file.
	 * If NULL is returned, then the Cancel Button was hit.
	 * @return
	 */
	public String getPath()
	{
		return this.sPath;
	}
}
